﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TimeTracker.Models;
using TimeTracker.Phone;

namespace TimeTracker.PhoneApp
{
    public class EntryViewModel : NotifyPropertyChangedBase
    {
        public string Id { get; set; }
        public string UserId { get; set; }
        public string ProjectId { get; set; }
        public string ClientId { get; set; }

        private string client;
        public string Client
        {
            get { return client; }
            set
            {
                this.client = value;
                Notify("Client");
                Notify("CompleteProject");
            }
        }

        private string project;
        public string Project
        {
            get { return project; }
            set
            {
                this.project = value;
                Notify("Project");
                Notify("CompleteProject");
            }
        }

        private string description;
        public string Description
        {
            get { return description; }
            set
            {
                this.description = value;
                Notify("Description");
            }
        }

        private DateTime lastStartTime;
        public DateTime LastStartTime
        {
            get { return lastStartTime; }
            set
            {
                this.lastStartTime = value;
                Notify("LastStartTime");
            }
        }

        private TimeSpan elapsedTime;
        public TimeSpan ElapsedTime
        {
            get { return elapsedTime; }
            set
            {
                this.elapsedTime = value;
                Notify("ElapsedTime");
            }
        }

        private bool isActive;
        public bool IsActive
        {
            get { return isActive; }
            set
            {
                this.isActive = value;
                Notify("IsActive");
                Notify("IsActiveImage");
            }
        }

        public string CompleteProject
        {
            get { return Project + "-" + Client; }
        }

        public EntryViewModel()
        {
            this.LastStartTime = DateTime.Now;
        }

        public EntryModel ToModel()
        {
            return new EntryModel
            {
                Id = Id,
                ClientId = ClientId,
                ProjectId = ProjectId,
                UserId = UserId,
                LastStartTime = LastStartTime,
                ElapsedTime = ElapsedTime,
                Description = Description,
                IsActive = IsActive
            };
        }

        public static EntryViewModel FromModel(EntryModel model)
        {
            return new EntryViewModel
            {
                Id = model.Id,
                UserId = model.UserId,
                ClientId = model.ClientId,
                ProjectId = model.ProjectId,
                Description = model.Description,
                LastStartTime = model.LastStartTime,
                ElapsedTime = model.ElapsedTime,
                Client = model.Client,
                Project = model.Project,
                IsActive = model.IsActive
            };
        }

        public void UpdateViewModel(EntryModel model)
        {
            ClientId = model.ClientId;
            ProjectId = model.ProjectId;
            UserId = model.UserId;
            LastStartTime = model.LastStartTime;
            ElapsedTime = model.ElapsedTime;
            Description = model.Description;
            Client = model.Client;
            Project = model.Project;
            IsActive = model.IsActive;
        }

        public EntryViewModel(EntryModel model)
        {
            this.Id = model.Id;
            this.UpdateViewModel(model);
        }

        public readonly MainViewModel Parent;

        public EntryViewModel(MainViewModel parent, EntryModel model)
        {
            this.Parent = parent;

            this.Id = model.Id;
            this.UpdateViewModel(model);
        }

        public void Toggle()
        {
            if (!isActive)
                Parent.Start(this);
            else
                Parent.Stop(this);
        }

        private readonly static ImageSource startedImage = LoadStaticImage("/Images/Started.png");
        private readonly static ImageSource stoppedImage = LoadStaticImage("/Images/Stopped.png");

        public ImageSource IsActiveImage
        {
            get { return this.IsActive ? startedImage : stoppedImage; }
        }

        private static ImageSource LoadStaticImage(string imageName)
        {
            return new BitmapImage(new Uri(imageName, UriKind.Relative));
        }
    }
}